package com.pronhub.cloud.shop.web.controller;

import com.pronhub.cloud.shop.domain.User;
import com.pronhub.cloud.shop.msg.SeckillCodeMsg;
import com.pronhub.cloud.shop.redis.MyRedisTemplate;
import com.pronhub.cloud.shop.redis.SeckillKeyPrefix;
import com.pronhub.cloud.shop.resolver.RedisValue;
import com.pronhub.shop.cloud.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * 防刷第一个控制器 随机的变量
 */
@RestController
@RequestMapping("path")
public class PathController {
    @Autowired
    private MyRedisTemplate myRedisTemplate;
    //就是生成一串字符然后给前面 最后在返回来
    @RequestMapping("/getPath")
    public Result<String> getPath(String uuid, Integer verifyCode, @RedisValue User user) {
        System.err.println("进来了");
        if(user==null){
            return Result.error(SeckillCodeMsg.OP_ERROR);
            //非法操作
        }
        //存放验证码的
        Integer result = myRedisTemplate.get(SeckillKeyPrefix.VERIFY_CODE_RESULT, uuid, Integer.class);
        //数字验证码的验证
        if(result==null || !result.equals(verifyCode)){
            return Result.error(SeckillCodeMsg.VERIFYCODE_ERROR);
        }
        String path = UUID.randomUUID().toString().replace("-","");
        //存放一个数字
        myRedisTemplate.set(SeckillKeyPrefix.SECKILL_PATH,uuid,path);

        return Result.success(path);
    }
}
